package lumis.service.uframe;

import java.io.IOException;

import lumis.portal.PortalContext;
import lumis.portal.PortalException;
import lumis.portal.UnexpectedException;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.serviceinterface.GenericServiceInterface;
import lumis.portal.serviceinterface.IServiceInterfaceRenderRequest;
import lumis.portal.serviceinterface.IServiceInterfaceRenderResponse;
import lumis.portal.serviceinterface.ServiceInterfaceException;
import lumis.portal.transaction.PortalTransactionFactory;
import lumis.util.ITransaction;

/**
 * Service interface that enables a uFrame div.
 * 
 * @version $Revision: 13092 $ $Date: 2011-05-28 18:19:06 -0300 (Sat, 28 May 2011) $
 * @since 6.0.0
 */
public class UFrameServiceInterface extends GenericServiceInterface
{
	/**
	 * Method that enables a uFrame div.
	 * 
	 * @since 6.0.0
	 */
	@Override
	public void render(IServiceInterfaceRenderRequest request, IServiceInterfaceRenderResponse response) throws ServiceInterfaceException, PortalException
	{
		try
		{
			ITransaction transaction = PortalTransactionFactory.createTransaction();
			try
			{
				transaction.begin();

				String url = ManagerFactory.getServiceInterfaceInstanceManager().getCustomProperty(request.getSessionConfig(), request.getServiceInterfaceInstanceId(), "url", transaction);

				if (url != null && url.length() > 0)
				{
					writeJavascriptDependencies(response);
					writeUFrameDiv(response, url);
				}
				else
				{
					writeBodyWithoutURL(request, response);
				}

				transaction.commit();
			}
			finally
			{
				transaction.dispose();
			}
		}
		catch (PortalException e)
		{
			throw e;
		}
		catch (Exception e)
		{
			throw new UnexpectedException(e);
		}
	}

	/**
	 * Writes a message on the body of the page when the URL is not defined.
	 * 
	 * @param request
	 *            HTTPRequest used to read the request data.
	 * @param response
	 *            HTTPResponse used to write the message.
	 * @throws Exception
	 *             If any error occurs writing the message.
	 * 
	 * @since 6.0.0
	 */
	private void writeBodyWithoutURL(IServiceInterfaceRenderRequest request, IServiceInterfaceRenderResponse response) throws Exception
	{
		if (request.getMode() == PortalContext.MODE_ADMIN)
		{
			String message = ManagerFactory.getLocalizationManager().localize("STR_SPECIFY_URL", new UFrameStringResource());
			response.getWriter().print(message);
		}
	}

	/**
	 * Writes the div that enables the inclusion of other pages.
	 * 
	 * @param response
	 *            HTTPResponse used to write the div.
	 * @throws IOException
	 *             If any error occurs writing the div.
	 * 
	 * @since 6.0.0
	 */
	private void writeUFrameDiv(IServiceInterfaceRenderResponse response, String url) throws IOException
	{
		response.getWriter().print("<div class=\"UFrame\" id=\"UFrame_" + response.getNamespace() + "\" src=\"" + url + "\" ></div>");
	}

	/**
	 * Writes the javascript dependencies of uFrame.
	 * 
	 * @param response
	 *            HTTPResponse used to write the dependencies.
	 * @throws IOException
	 *             If any error occurs writing the dependencies.
	 * 
	 * @since 6.0.0
	 */
	private void writeJavascriptDependencies(IServiceInterfaceRenderResponse response) throws IOException
	{
		response.getBeforeWriter().print("<script src=\"lumis/service/uframe/jquery-1.2.3.min.js\" type=\"text/javascript\"></script>", "jquery-1.2.3.min.js");
		response.getBeforeWriter().print("<script src=\"lumis/service/uframe/htmlparser.js\" type=\"text/javascript\"></script>", "htmlparser.js");
		response.getBeforeWriter().print("<script src=\"lumis/service/uframe/UFrame-1.0.0.js\" type=\"text/javascript\"></script>", "UFrame-1.0.0.js");
	}
}
